# -*- coding:utf-8 -*-

import pygame
from pygame.locals import *  # 包含pygame使用的各种常量
import time,random

# 添加玩家子弹，并实现发射。

class HeroPlane():
    ''' 玩家飞机类 '''
    def __init__(self, screen_temp):
        self.x = 200
        self.y = 400
        self.screen = screen_temp
        self.image = pygame.image.load("./images/me.png")
        self.bullet_list = []
        self.bomb = pygame.image.load("./images/bomb.png")


    def display(self):
        ''' 绘制飞机 '''

        #遍历移动子弹
        for bullet in self.bullet_list:
            bullet.display()
            #移动子弹，并判断是否越界。
            if bullet.move():
                self.bullet_list.remove(bullet)

        self.screen.blit(self.image, (self.x, self.y))


    def move_left(self):
        ''' 左移动,并判断防止越界 '''
        self.x -= 5
        if self.x<0:
            self.x=0

    def move_right(self):
        ''' 右移动,并判断防止越界 '''
        self.x += 5
        if self.x > 406:
            self.x = 406
    def fire(self):
        self.bullet_list.append(Bullet(self.screen, self.x, self.y))
        print(len(self.bullet_list))

    def hurt(self):
        self.screen.blit(self.bomb,(self.x,self.y))

class Bullet():
    ''' 玩家子弹类 '''
    def __init__(self, screen_temp, x, y):
        self.x = x+51
        self.y = y
        self.screen = screen_temp
        self.image = pygame.image.load("./images/pd.png")

    def display(self):
        self.screen.blit(self.image, (self.x, self.y))

    def move(self):
        self.y-=10
        if self.y<-20:
            return True

class EnemyBullet():
    '''敌机子弹类'''
    def __init__(self,screen_temp,x,y):
        self.x = x+58
        self.y = y+78
        self.screen = screen_temp
        self.image = pygame.image.load("./images/me.png")
        self.bullet_enemy_list =[]

    def display(self):
        self.screen.blit(self.image, (self.x, self.y))

    def move(self):
        self.y+=10
        if self.y>568:
            return True


class EnemyPlane():
    ''' 敌机 '''
    def __init__(self, screen_temp):
        self.x = random.choice(range(0,408))
        self.y = -75
        self.screen = screen_temp
        self.image = pygame.image.load("./images/e2.png")
        self.bomb = pygame.image.load("./images/bomb.png")
        self.bullet_list = []


    def display(self):
        self.screen.blit(self.image, (self.x, self.y))
        for b in self.bullet_list:
            b.display()
            if b.move():
                self.bullet_list.remove(b)

    # 敌机发射子弹
    def fire(self):
        num = random.randrange(10)
        if num == 1:
            self.bullet_list.append(EnemyBullet(self.screen,self.x,self.y))

    def move(self,hero):
        self.y+= 10
        if self.y >568:
            return True



        '''
        # 飞机碰撞检测
        if self.x > hero.x - 50 and self.x < hero.x + 50 and self.y > hero.y - 40 and self.y < hero.y + 40:
            bomb_image = './images/bomb.png'
            self.image = pygame.image.load(bomb_image)
        # 判断是否越界
        if self.y >512:
            return True
        '''

        # 敌机中弹
        for bo in hero.bullet_list:
            if bo.x>self.x+12 and bo.x<self.x+92 and bo.y>self.y+20 and bo.y<self.y+60 :
                hero.bullet_list.remove(bo)
                self.screen.blit(self.bomb,(self.x,self.y+40))
                return True





def key_control(hero_temp):
    ''' 键盘控制函数 '''

    #获取键盘控制事件
    for event in pygame.event.get():
        #判断是否是点击了退出按钮
        if event.type == QUIT:
            print("exit")
            exit()


    pressed_keys = pygame.key.get_pressed()

    if pressed_keys[K_LEFT] or pressed_keys[K_a]:
        print('left')
        hero_temp.move_left()

    elif pressed_keys[K_RIGHT] or pressed_keys[K_d]:
        print('right')
        hero_temp.move_right()

    if pressed_keys[K_SPACE]:
        print('space')
        hero_temp.fire()

def main():
    '''主程序'''

    # 定义背景\
    screen = pygame.display.set_mode((512,568),0,0)

    # 创建一个游戏背景图片(512*1536)
    background = pygame.image.load("./images/bg2.jpg")
    m=-968

    # 创建一个玩家飞机对象
    hero = HeroPlane(screen)
    # 定义一个存放敌机的列表
    enemy_list = []

    while True:
        # 绘制背景图
        screen.blit(background,(0,m))
        m+=2
        if m>=-200:
            m = -968

         # 显示玩家飞机
        hero.display()
        # 键盘控制（负责移动玩家）
        key_control(hero)
        # 判断英雄飞机是否中弹
        for enemy in enemy_list:
            if (enemy.x + 116 > hero.x and enemy.y + 82 > hero.y and enemy.x < hero.x + 106 and enemy.y < hero.y + 76):
                hero.hurt()

            for bo in enemy.bullet_list:
                if (bo.x > hero.x + 12 and bo.x < hero.x + 92 and bo.y > hero.y + 20 and bo.y < hero.y + 60):
                    enemy.bullet_list.remove(bo)
                    hero.hurt()

        # 随机显示敌机
        if random.choice(range(50))==10:
            enemy_list.append(EnemyPlane(screen))

        # 遍历所有敌机，显示敌机，并与玩家子弹碰撞检测
        for em in enemy_list:
            em.display()
            # 敌机发射子弹
            if random.choice(range(20)) == 10:
                em.fire()
            # 判断
            if em.move(hero):
                enemy_list.remove(em)


        # 更新屏幕
        pygame.display.update()

        # 定时
        time.sleep(0.04)

# 判断当前是否是主程序，若是就执行主程序。
if __name__ == "__main__":
    main()
